<?php


namespace app\admin\controller\count;


use app\common\controller\Adminbase;
use think\Db;
use think\facade\Session;

class Business extends Adminbase
{
//    protected $searchFields = 'name';
    public function index()
    {
        $this->admin1 = Session::get("admin");
        if ($this->admin1['roleid'] == 3) {
            $where[] = ['id', '=', $this->admin1['id']];
        }
        $where2=array();
        if ($this->admin1['roleid'] == 4) {
            $where2[] = ['id', '=', $this->admin1['id']];
            $where[] = ['area_id', '=', $this->admin1['area_id']];
        }
        //todo 设置权限以后做
        $where1[] = ['is_del', '=', 0];
        $where[] = ['roleid', '=', 3];
        $where1[] = ['status', '=', 1];
        $list = array();
        $count = 0;
        $feng_list = Db::name('admin')->field('id,nickname as catname,roleid,area_id')
            ->order('area_id')
            ->where($where1)
            ->where($where)
            ->select();
        foreach ($feng_list as $k => $v) {
            $list[$count]['parentid'] = 0;
            $list[$count]['target'] = 'right';
            $list[$count]['id'] = $v['id'];
            $list[$count]['catname'] = $v['catname'];
            $list[$count]['isParent'] = true;
            $count++;
            $son = Db::name('admin')
                ->field('id,nickname as catname,roleid,area_id')
                ->where($where1)
                ->where($where2)
                ->where('roleid', '=', 4)
                ->where('area_id', '=', $v['area_id'])
                ->select();
            if ($son) {
                foreach ($son as $k2 => $v2) {
                    $list[$count]['parentid'] = $v['id'];
                    $list[$count]['target'] = 'right';
                    $list[$count]['id'] = $v2['id'];
                    $list[$count]['catname'] = $v2['catname'];
//                    $list[$count]['url'] = url('detail', ['ying_id' => $v2['id']]);
                    $list[$count]['isParent'] = true;
                    $count++;
                    $business_list = Db::name('business')->where('belong_admin_id', '=', $v2['id'])->select();
                    if ($business_list) {
                        foreach ($business_list as $k3 => $v3) {
                            $list[$count]['parentid'] = $v2['id'];
                            $list[$count]['target'] = 'right';
                            $list[$count]['id'] = $count;
                            $list[$count]['catname'] = $v3['business_name'];
                            $list[$count]['url'] = url('detail', ['business_id' => $v3['id']]);
                            $count++;
                        }
                    }
                }
            }
        }
        $this->assign('json', json_encode($list));

        return $this->fetch('count/ying/index');
    }

    public function detail()
    {
        $data = $this->request->param();
        if ($this->request->isAjax()) {
            $filter = input('filter');
            $goods_where[] = ['is_del', '<>', 1];
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    $create_time[0] = strtotime($create_time[0]);
                    $create_time[1] = strtotime($create_time[1]);
                }
                if (isset($filter['name'])) {
                    $goods_where[] = ['name', 'like', '%' . $filter['name'] . '%'];
                }
            }
            $_list = Db::name('goods')
                ->order(array('id' => 'desc'))
                ->where($goods_where)
                ->select();
            //todo 分公司下所有运营中心 运营中心下所有所有服务站的订单
            $order_where[] = ['business_id', '=', $data['business_id']];
            $order_where[] = ['pay_status', '=', 1];
            if (isset($create_time)) {
                $order_where[] = ['pay_time', 'between', [$create_time[0], $create_time[1]]];
            }
            $order_ids = Db::name('order')->where($order_where)->column('id');
            $order_detail_where[] = ['order_id', 'in', $order_ids];
            //订单付款就可以
            $subtotal_sum_zong = 0;
            $hexiao_zong = 0;
            $pingtai_zong = 0;
            $write_price_sum_zong=0;
            foreach ($_list as $k => $v) {
                //销售额
                $subtotal_sum = Db::name('order_detail')->where('goods_id', $v['id'])->where($order_detail_where)->sum('subtotal');
                //总核销金额
                $write_price_sum = Db::name('order_detail')->where('goods_id', $v['id'])->where($order_detail_where)->sum('write_price');
                //总订货金额
                $purchase_price_sum = Db::name('order_detail')->where('goods_id', $v['id'])->where($order_detail_where)->sum('purchase_price');
                //核销利差
                $hexiao = $write_price_sum - $purchase_price_sum;
                //平台利差
                $pingtai = $subtotal_sum - $write_price_sum;
                $_list[$k]['subtotal_sum'] = $subtotal_sum;
                $_list[$k]['hexiao'] = $hexiao;
                $_list[$k]['pingtai'] = $pingtai;
                $_list[$k]['business_id'] = $data['business_id'];
                $_list[$k]['write_price_sum'] = $write_price_sum;
                $subtotal_sum_zong += $subtotal_sum;
                $hexiao_zong += $hexiao;
                $pingtai_zong += $pingtai;
                $write_price_sum_zong+=$write_price_sum;
            }
            $count = Db::name('goods')
                ->order(array('id' => 'desc'))
                ->where('is_del', '=', 0)->count();
            $_list[$count]['name'] = '总计';
            $_list[$count]['subtotal_sum'] = $subtotal_sum_zong;
            $_list[$count]['hexiao'] = $hexiao_zong;
            $_list[$count]['pingtai'] = $pingtai_zong;
            $_list[$count]['write_price_sum'] = $write_price_sum_zong;
            $result = ["code" => 0, "count" => $count, "data" => $_list];
            return json($result);
        }
        $this->assign('business_id', $data['business_id']);
        return $this->fetch();
    }

    public function riDetail()
    {
        $data = $this->request->param();
        if ($this->request->isAjax()) {
            $goods_info = Db::name('goods')->find($data['goods_id']);
            $business_info = Db::name('business')->find($data['business_id']);
            $order_where[] = ['business_id', '=', $data['business_id']];
            $order_where[] = ['pay_status', '=', 1];
            $data_list = array();
            $subtotal_sum_zong = 0;
            $hexiao_zong = 0;
            $pingtai_zong = 0;
            $count = 20;
            $filter = input('filter');
            $filter = json_decode($filter, true);
            if (isset($filter['date'])) {
                $create_time = explode(' - ', $filter['date']);
                $all_days = getTimestampsInDateRange($create_time[0], $create_time[1]);
                $count = count($all_days);
                foreach ($all_days as $j => $v) {
                    $order_ids = Db::name('order')
                        ->where('pay_time', 'between', [$v, $v + 86399])
                        ->where($order_where)
                        ->column('id');
                    //销售额
                    $subtotal_sum = Db::name('order_detail')->where('goods_id', $data['goods_id'])->where('order_id', 'in', $order_ids)->sum('subtotal');
                    //总核销金额
                    $write_price_sum = Db::name('order_detail')->where('goods_id', $data['goods_id'])->where('order_id', 'in', $order_ids)->sum('write_price');
                    //总订货金额
                    $purchase_price_sum = Db::name('order_detail')->where('goods_id', $data['goods_id'])->where('order_id', 'in', $order_ids)->sum('purchase_price');
                    //核销利差
                    $hexiao = $write_price_sum - $purchase_price_sum;
                    //平台利差
                    $pingtai = $subtotal_sum - $write_price_sum;
                    $data_list[$j]['id'] = $j + 1;
                    $data_list[$j]['date'] = date('m月d日', $v);
                    $data_list[$j]['unit_name'] = $business_info['business_name'];
                    $data_list[$j]['goods_name'] = $goods_info['name'];
                    $data_list[$j]['subtotal_sum'] = $subtotal_sum;
                    $data_list[$j]['hexiao'] = $hexiao;
                    $data_list[$j]['pingtai'] = $pingtai;
                    $data_list[$j]['feng_id'] = $data['ying_id'];
                    $subtotal_sum_zong += $subtotal_sum;
                    $hexiao_zong += $hexiao;
                    $pingtai_zong += $pingtai;
                }
            } else {
                for ($i = $count - 1, $j = 0; $i >= 0; $i--, $j++) {
                    $timescope = getTimestamp(-$i);
                    $order_ids = Db::name('order')
                        ->where('pay_time', 'between', [$timescope[0], $timescope[1]])
                        ->where($order_where)
                        ->column('id');
                    //销售额
                    $subtotal_sum = Db::name('order_detail')->where('goods_id', $data['goods_id'])->where('order_id', 'in', $order_ids)->sum('subtotal');
                    //总核销金额
                    $write_price_sum = Db::name('order_detail')->where('goods_id', $data['goods_id'])->where('order_id', 'in', $order_ids)->sum('write_price');
                    //总订货金额
                    $purchase_price_sum = Db::name('order_detail')->where('goods_id', $data['goods_id'])->where('order_id', 'in', $order_ids)->sum('purchase_price');
                    //核销利差
                    $hexiao = $write_price_sum - $purchase_price_sum;
                    //平台利差
                    $pingtai = $subtotal_sum - $write_price_sum;
                    $data_list[$j]['id'] = $j + 1;
                    $data_list[$j]['date'] = date('m月d日', strtotime(-$i . " days"));
                    $data_list[$j]['unit_name'] =$business_info['business_name'];
                    $data_list[$j]['goods_name'] = $goods_info['name'];
                    $data_list[$j]['subtotal_sum'] = $subtotal_sum;
                    $data_list[$j]['hexiao'] = $hexiao;
                    $data_list[$j]['pingtai'] = $pingtai;
                    $data_list[$j]['feng_id'] = $data['ying_id'];
                    $subtotal_sum_zong += $subtotal_sum;
                    $hexiao_zong += $hexiao;
                    $pingtai_zong += $pingtai;
                }
            }
            $data_list[$count]['unit_name'] = '总计';
            $data_list[$count]['subtotal_sum'] = $subtotal_sum_zong;
            $data_list[$count]['hexiao'] = $hexiao_zong;
            $data_list[$count]['pingtai'] = $pingtai_zong;
            $result = ["code" => 0, "count" => $count, "data" => $data_list];
            return json($result);
        }
        $this->assign('business_id', $data['business_id']);
        $this->assign('goods_id', $data['goods_id']);
        return $this->fetch('ridetail');
    }
}